package it.polimi.travelDreamEJB.travelpacketmanagement;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.ejb.EJBContext;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import it.polimi.travelDreamEJB.basicproductmanagement.BasicProductMgrBean;
import it.polimi.travelDreamEJB.basicproductmanagement.Excursion;
import it.polimi.travelDreamEJB.basicproductmanagement.Flight;
import it.polimi.travelDreamEJB.basicproductmanagement.Hotel;
import it.polimi.travelDreamEJB.basicproductmanagement.dto.ExcursionDTO;
import it.polimi.travelDreamEJB.personalizedpacketmanagement.PersonalizedPacketMgr;
import it.polimi.travelDreamEJB.personalizedpacketmanagement.dto.PersonalizedPacketDTO;
import it.polimi.travelDreamEJB.personalizedpacketmanagement.dto.SearchPersonalizedPacketDTO;
import it.polimi.travelDreamEJB.usermanagement.User;
import it.polimi.travelDreamEJB.usermanagement.UserMgrBean;
import it.polimi.travelDreamEJB.usermanagement.dto.UserDTO;

@Stateless
public class PersonalizedPacketMgrBean implements PersonalizedPacketMgr {

	@PersistenceContext
	private EntityManager em;

	@Resource
	private EJBContext context;

	@Override
	public void save_personalized_packet(
			PersonalizedPacketDTO personalized_packet) {
		PersonalizedPacket pp = new PersonalizedPacket(personalized_packet);
		List<Excursion> excursions = new ArrayList<Excursion>();

		for (ExcursionDTO e : personalized_packet.getExcursions()) {
			excursions.add(new Excursion(e));
		}

		pp.setUser(em
				.find(User.class, personalized_packet.getUser().getEmail()));
		pp.setHotel(new Hotel(personalized_packet.getHotel()));
		pp.setDepartureFlight(new Flight(personalized_packet
				.getDepartureFlight()));
		pp.setReturnFlight(new Flight(personalized_packet.getReturnFlight()));
		pp.setExcursions(excursions);

		em.persist(pp);
	}
	
	@Override
	public void save_and_buy_personalized_packet(
			PersonalizedPacketDTO personalizedPacket) {
		List<Excursion> excursions = new ArrayList<Excursion>();
		PersonalizedPacket pp = new PersonalizedPacket(personalizedPacket);
		for (ExcursionDTO e : personalizedPacket.getExcursions()) {
			excursions.add(new Excursion(e));
		}
		User user = em.find(User.class, personalizedPacket.getUser().getEmail());
		pp.setUser(user);
		pp.setIdPersonalizedPacket(0);
		pp.setHotel(new Hotel(personalizedPacket.getHotel()));
		pp.setDepartureFlight(new Flight(personalizedPacket
				.getDepartureFlight()));
		pp.setReturnFlight(new Flight(personalizedPacket.getReturnFlight()));
		pp.setExcursions(excursions);
		pp.getBuyer().add(user);

		em.persist(pp);
		
	}

	@Override
	public void update_personalized_packet(
			PersonalizedPacketDTO personalized_packet) {
		em.merge(new PersonalizedPacket(personalized_packet));

	}

	@Override
	public void delete_personalized_packet(
			PersonalizedPacketDTO personalized_packet) {
		PersonalizedPacket pp = em.find(PersonalizedPacket.class,
				personalized_packet.getId_personalized_packet());
		em.remove(pp);
	}

	@Override
	public PersonalizedPacketDTO getPersonalizedPacketDTO() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<PersonalizedPacketDTO> findPersonalizedPacket(
			SearchPersonalizedPacketDTO searchPersonalizedPacket) {
		List<PersonalizedPacketDTO> ppDTOList = new ArrayList<PersonalizedPacketDTO>();
		List<PersonalizedPacket> ppList = new ArrayList<PersonalizedPacket>();
		Query query = em.createNamedQuery(
				"PersonalizedPacket.findPersonalizedPacket",
				PersonalizedPacket.class);
		query.setParameter("email", searchPersonalizedPacket.getUser()
				.getEmail());
		ppList = query.getResultList();
		for (PersonalizedPacket pp : ppList) {

			ppDTOList.add(convertPersonalizedPacketToDTO(pp));

		}

		return ppDTOList;
	}

	static public PersonalizedPacketDTO convertPersonalizedPacketToDTO(
			PersonalizedPacket pp) {
		PersonalizedPacketDTO ppDTO = new PersonalizedPacketDTO();
		ppDTO.setId_personalized_packet(pp.getIdPersonalizedPacket());
		ppDTO.setUser(UserMgrBean.convertToDTO(pp.getUser()));
		ppDTO.setHotel(BasicProductMgrBean.convertHotelToDTO(pp.getHotel()));
		ppDTO.setDepartureFlight(BasicProductMgrBean.convertFlightToDTO(pp
				.getDepartureFlight()));
		ppDTO.setReturnFlight(BasicProductMgrBean.convertFlightToDTO(pp
				.getReturnFlight()));
		List<ExcursionDTO> excursionDTOList = new ArrayList<ExcursionDTO>();
		for (Excursion e : pp.getExcursions()) {
			excursionDTOList.add(BasicProductMgrBean.convertExcursionToDTO(e));
		}

		ppDTO.setExcursions(excursionDTOList);

		return ppDTO;
	}

	@Override
	public void buyPacket(PersonalizedPacketDTO packet, UserDTO buyer) {
		PersonalizedPacket pp = em.find(PersonalizedPacket.class,
				packet.getId_personalized_packet());
		User u = em.find(User.class, buyer.getEmail());

		pp.getBuyer().add(u);
		em.merge(pp);

	}
	
	@Override
	public void inviteAtPacket(PersonalizedPacketDTO packet, String email) {
		PersonalizedPacket pp = em.find(PersonalizedPacket.class, packet.getId_personalized_packet());
		User u = em.find(User.class, email);
		if ( u != null ){
			
			pp.getGuest().add(u);
			em.merge(pp);
			
		}
		
	}
	
	public List<PersonalizedPacketDTO> findBoughtPacket(
			SearchPersonalizedPacketDTO searchPersonalizedPacket) {
		List<PersonalizedPacketDTO> ppDTOList = new ArrayList<PersonalizedPacketDTO>();
		List<PersonalizedPacket> ppList = new ArrayList<PersonalizedPacket>();
		Query query = em.createNamedQuery("PersonalizedPacket.findBoughtPacket",
				PersonalizedPacket.class);
		query.setParameter("email", searchPersonalizedPacket.getUser()
				.getEmail());
		ppList = query.getResultList();
		for (PersonalizedPacket pp : ppList) {

			ppDTOList.add(convertPersonalizedPacketToDTO(pp));

		}

		return ppDTOList;
	}
	
	public List<PersonalizedPacketDTO> findGuestPacket(
			SearchPersonalizedPacketDTO searchPersonalizedPacket) {
		List<PersonalizedPacketDTO> ppDTOList = new ArrayList<PersonalizedPacketDTO>();
		List<PersonalizedPacket> ppList = new ArrayList<PersonalizedPacket>();
		Query query = em.createNamedQuery("PersonalizedPacket.findGuestPacket",
				PersonalizedPacket.class);
		query.setParameter("email", searchPersonalizedPacket.getUser()
				.getEmail());
		ppList = query.getResultList();
		for (PersonalizedPacket pp : ppList) {

			ppDTOList.add(convertPersonalizedPacketToDTO(pp));

		}

		return ppDTOList;
	}

}
